package com.ys.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ys.extend.common.exception.BusinessRuntimeException;
import com.ys.extend.common.model.AjaxJson;
import com.ys.web.pojo.IndustryMsgInfo;
import com.ys.web.remotemodel.RemoteIndustryInfo;
import com.ys.web.service.IndustryService;

/**
 * 行业信息
 * @author wdj
 *
 */

@Controller
@RequestMapping("/industryController")
public class IndustryController {

	@Autowired
	private IndustryService industryService;

	private String message;
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}		
	
	@RequestMapping(params="showIndustry")
	public ModelAndView showIndustry(HttpServletRequest request){
		
		return new ModelAndView("industry/industry");
	}
	
	/**
	 * 获取全部行业以及对应的四象限信息（含分页）
	 * @param page 当前页码
	 * @return 全部行业以及对应的四象限信息（含分页）
	 */
	@RequestMapping(params="getALLIndustryByPage")
	@ResponseBody
	public List<IndustryMsgInfo> getALLIndustryByPage(Integer page){
		Integer currentPage = 1;
		if(page!=null) {
			currentPage = page;
		}
		
		List<IndustryMsgInfo> list = industryService.getALLIndustryByPage(currentPage);
		
		return list;
	}

	/**
	 * 添加或者修改行业对应的四象限信息
	 * @param industryQuadrantInfo
	 * @return
	 */
	@RequestMapping(params="doAddOrUpdate")
	@ResponseBody
	public AjaxJson doAddOrUpdate(@RequestBody IndustryMsgInfo industryQuadrantInfo) {
		AjaxJson j = new AjaxJson();
		message = "success";
		try {									
			//执行新增或者更新
			industryService.addOrUpdateQuadrantInfo(industryQuadrantInfo);
			
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
			message = "fail";
			throw new BusinessRuntimeException(e.getMessage());
		}
		
		j.setMsg(message);
		return j;
	}	
	
	/**
	 * 删除行业以及对应的四象限信息
	 * @param industryId
	 * @return
	 */
	@RequestMapping(params="doDelete")
	@ResponseBody
	@Deprecated
	public AjaxJson doDelete(Integer industryId) {
		AjaxJson j = new AjaxJson();
		message = "删除行业成功";
		try {
			industryService.deleteIndustryQuadrantInfo(industryId);
			
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
			message = "删除行业失败，异常";
			throw new BusinessRuntimeException(e.getMessage());
		}
		
		j.setMsg(message);
		return j;
	}	

	/**
	 * 获取全部行业信息
	 * @return 全部行业信息列表
	 */
	@RequestMapping(params="getAllIndustry")
	@ResponseBody
	public List<RemoteIndustryInfo> getAllIndustry() {
		List<RemoteIndustryInfo> list = industryService.getALLIndustryList();		
		return list;
	}

	/**
	 * 判断是否存在相同行业
	 * @param industryId 行业id
	 * @param industryName 行业名称
	 * @return true：不存在相同行业；false：存在相同行业
	 */
	@RequestMapping(params="ifExistIndustry")
	@ResponseBody
	@Deprecated
	public AjaxJson ifExistIndustry(String industryId, String industryName) {
		AjaxJson j = new AjaxJson();
		
		Integer industryId_par = null;
		if(industryName!=null && industryName.length()>0){
			 if(industryId!=null && industryId.length()>0) {
				 industryId_par = Integer.valueOf(industryId);				 
			 }
			 Boolean ifExist = industryService.ifExistIndustry(industryId_par, industryName);			 
			 if(!ifExist) {				
				 message = "存在相同行业";
				 j.setSuccess(false);
			 }
		}
		else {
			message = "参数为空！";
			j.setSuccess(false);
			try {
				throw new Exception("参数为空！");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		j.setMsg(message);		
		return j;
	}
	

		
	
		
}
